package com.qingmeng.project.model.entity;

import cn.hutool.json.JSONUtil;
import com.qingmeng.project.model.vo.InterfaceInfoVo;
import com.qingmeng.project.service.CountInterfaceService;
import com.qingmeng.project.utils.SpringUtils;
import lombok.extern.slf4j.Slf4j;
import org.quartz.Job;
import org.quartz.JobDataMap;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.stereotype.Component;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@Component
@Slf4j
public class InterfaceCountJob implements Job{
//
//    @Resource
//    private CountInterfaceService countInterfaceService;

    @Override
    public void execute(JobExecutionContext context) {

        // 获取当前时间
        Date date = new Date();
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        获取 JobDetail 包装Job时设置的数据集合 JobDataMap
//        JobDataMap 通过map 的k-v 的方式保存数据
        JobDataMap dataMap = context.getJobDetail().getJobDataMap();
        int limit = dataMap.getInt("limit");
//        int limit = dataMap.getIntValue("limit");
        System.out.println(sf.format(date) + "  定时查询接口调用TOP " + limit + "执行了，");

        CountInterfaceService countInterfaceService = SpringUtils.getBean(CountInterfaceService.class);
        List<InterfaceInfoVo> interfaceInfoVos = countInterfaceService.countInterface(limit);
        if (interfaceInfoVos == null){
            log.error("调用接口统计失败");
        }
        //遍历输出一下
        for (InterfaceInfoVo interfaceInfoVo : interfaceInfoVos) {
            //将对象转换成JSON字符串
            System.out.println(JSONUtil.toJsonStr(interfaceInfoVo));
            System.out.println();
        }
    }
}


